package com.shundr.shipper.cargo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.DialogCallPhoneNumber;
import com.shundr.shipper.common.view.DialogCallPhoneNumber.OnCallPhoneDialogListener;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.user.UserAuthActivity;
import com.shundr.shipper.user.UserReAuthActivity;
import com.shundr.shipper.user.http.UserRequest;

/**
 * 给车主打电话公共类
 * 
 * @author Administrator
 *
 */
public class ContactShipper {
	private Context mContext;
	private int mIdValiStatus;
	String mPhone[];
	int nums;
	private OnCanCallListener mOnCanCallListener;

	public interface OnCanCallListener{
		public void onCanCallListener();
	}
	public ContactShipper(Context context, int idValiStatus) {
		this.mContext = context;
		this.mIdValiStatus = idValiStatus;
		getQueryNumValidation();
	}

	public void setmOnCanCallListener(OnCanCallListener mOnCanCallListener) {
		this.mOnCanCallListener = mOnCanCallListener;
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.GET_QueryNumValidationByUser_SUCCESSED:
				try {
					String num = (String) msg.obj;
					if (StringUtil.isNumeric(num)) {
						nums = Integer.valueOf(num);
						if (nums > 0) {
							HaveCallPhoneNum();
						} else {
							CallPhoneNumZero();
						}
					} else {
						ToastHelper.toastShort(mContext, "获取失败");    
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				break;
			case UserRequest.REQUEST_FAILED:
				try {
					String description = (String) msg.obj;
					ToastHelper.toastShort(mContext, description);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	/***
	 * 获取可打电话次数
	 */
	private void getQueryNumValidation() {
		new UserRequest(mContext, mHandler).getQueryNumValidationByUser();
	}
	
	 DialogCallPhoneNumber mCallPhoneNumber;
	 
	 private void OnCallPhoneToShipper(){
		 mOnCanCallListener.onCanCallListener();
	 }
	 
	/**
	 * 可打电话次数大于零
	 */
	private void HaveCallPhoneNum(){
		switch (mIdValiStatus) {
		case 0:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
					mContext.startActivity(new Intent(mContext, UserAuthActivity.class));
				}
				@Override
				public void onSecond() {
					OnCallPhoneToShipper();
				}	
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;
		case 1:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
					OnCallPhoneToShipper();
				}
				@Override
				public void onSecond() {
				}
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;
		case 3:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
					mContext.startActivity(new Intent(mContext, UserReAuthActivity.class));
				}
				@Override
				public void onSecond() {
					OnCallPhoneToShipper();
				}
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;	
		}
	}

	/**
	 * 可打电话次数为零
	 */
	private void CallPhoneNumZero() {
		switch (mIdValiStatus) {
		case 0:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
					mContext.startActivity(new Intent(mContext, UserAuthActivity.class));
				}
				@Override
				public void onSecond() {
					
				}
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;
		case 1:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
				}
				@Override
				public void onSecond() {
				}
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;
		case 3:
			mCallPhoneNumber=new DialogCallPhoneNumber(mContext, mIdValiStatus, nums, new OnCallPhoneDialogListener() {
				@Override
				public void onFirst() {
					mContext.startActivity(new Intent(mContext, UserReAuthActivity.class));
				}
				@Override
				public void onSecond() {

				}
			});
			mCallPhoneNumber.setCanceledOnTouchOutside(true);
			mCallPhoneNumber.show();
			break;		
		}
	}

}



